package com.lanchetech.merchant.controller;

import com.lanchetech.bean.request.CardConsumptionFundItemReq;
import com.lanchetech.bean.request.CardConsumptionFundReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.CardConsumptionFundItemVo;
import com.lanchetech.bean.vo.CardConsumptionFundVo;
import com.lanchetech.common.enums.AccessType;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.CardConsumptionFundItem;
import com.lanchetech.service.CardConsumptionFundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "商户消费金管理", tags = "商户消费金管理")
@RestController
@RequestMapping("/api/card")
public class MerchantCardFundController {

    @Autowired
    private CardConsumptionFundService cardConsumptionFundService;


    @PostMapping("/list")
    @ApiOperation("消费金列表")
    @PreAuthorize("hasAuthority('marketing.card-apply.card-apply-info')")
    public ResultData<BasePageResp<CardConsumptionFundVo>> list(@RequestBody CardConsumptionFundReq cardConsumptionFundReq) {
        ResultData<BasePageResp<CardConsumptionFundVo>> vo = cardConsumptionFundService.list(cardConsumptionFundReq);
        return vo;
    }

    @PostMapping("/apply")
    @ApiOperation("申请消费金商品")
    @PreAuthorize("hasAuthority('marketing.card-apply.card-apply-info.apply.auth')")
    public BaseResp apply(@RequestBody List<CardConsumptionFundItem> cardConsumptionFundItems) {
        BaseResp baseResp = new BaseResp();
        List<String> strings = new ArrayList<>();
        try {
            cardConsumptionFundService.apply(cardConsumptionFundItems, strings);
            baseResp.setErrorCodes(strings);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @GetMapping("/getBindSpu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页页码,从1开始", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "每页页数", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "cardConsumptionFundId", value = "消费金标识", dataType = "Integer", paramType = "body", required = true),
            @ApiImplicitParam(name = "status", value = "审核状态(0:待审核  1:审核通过  2:审核拒绝)", dataType = "Byte", paramType = "body"),
            @ApiImplicitParam(name = "spuId", value = "商品编号", dataType = "Long", paramType = "body")
    })
    @PreAuthorize("hasAuthority('marketing.card-list.bindspu')")
    @ApiOperation("获取消费金商品")
    public ResultData<BasePageResp<CardConsumptionFundItemVo>> getBindSpu(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                          @RequestParam(name = "cardConsumptionFundId") Long cardConsumptionFundId,
                                                                          @RequestParam(name = "status", required = false) Byte status,
                                                                          @RequestParam(name = "spuId", required = false) Long spuId) {
        CardConsumptionFundItemReq req = new CardConsumptionFundItemReq();
        req.setSpuId(spuId);
        req.setPageSize(pageSize);
        req.setPageNo(pageNo);
        req.setStatus(status);
        req.setCardConsumptionFundId(cardConsumptionFundId);
        req.setAccessType(AccessType.MERCHANT);
        ResultData<BasePageResp<CardConsumptionFundItemVo>> page = cardConsumptionFundService.getBindSpu(req);
        return page;
    }

    @PostMapping("/deleted/{id}")
    @PreAuthorize("hasAuthority('marketing.card-apply.card-apply-info.deleted.auth')")
    @ApiOperation("解绑商品")
    public BaseResp deleted(@PathVariable Long id) {
        BaseResp baseResp = new BaseResp();
        try {
            cardConsumptionFundService.deleted(id);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }
}
